from tools.mysqlconntool import mysqlconn

table_name = 'user_psychological_test_record'

def read():
    sql = 'SELECT * FROM user_psychological_test_record ORDER BY id DESC'
    users = mysqlconn.sql_select(sql)
    return users

def id_delete(id):
    record_id = id  # 替换为要删除的记录 ID
    count = mysqlconn.delete_record(table_name, record_id)
    return count

def add(data):
    count = mysqlconn.insert_record(table_name, data)
    return count

def update(data, id):
    count = mysqlconn.update_record(table_name, id, data)
    return count

def num_info(user_id):
    connection = mysqlconn.get_connection()
    cursor = connection.cursor()
    sql = f'SELECT * FROM user_psychological_test_record WHERE user_id = %s ORDER BY id DESC'
    val = (user_id,)  # 注意这里是一个元组，即使只有一个值也要加上逗号
    print(f"查询表：{table_name}, 查询ID：{val}")

    cursor.execute(sql, val)

    # 读取查询结果
    result = cursor.fetchall()
    return result

def importing(datas):
    print(datas)
    print(len(datas))
    count = 0
    for data in datas:
        print(f"循环后的data = {data}")
        data.pop("id")
        print(f"去掉id后的data = {data}")

        mysqlconn.insert_record(table_name, data)
        count += 1
    print("count = ", count)
    return count

